<template>
  <div class="register-container">
    <div class="login-box">
      <div class="login-header">
        <h1>用户注册</h1>
        <p>请填写以下信息完成注册</p>
      </div>
      <form class="login-form" @submit.prevent="register">
        <!-- 两列布局的表单行 -->
        <div class="form-row">
          <!-- 用户名 -->
          <div class="form-group">
            <label for="reg-username" class="form-label">用户名</label>
            <div class="input-wrapper">
              <i class="icon-user"></i>
              <input
                  type="text"
                  id="reg-username"
                  v-model="user.username"
                  required
                  class="form-input"
                  placeholder="请输入用户名"
              >
            </div>
          </div>

          <!-- 昵称 -->
          <div class="form-group">
            <label for="reg-nickname" class="form-label">昵称</label>
            <div class="input-wrapper">
              <i class="icon-user"></i>
              <input
                  type="text"
                  id="reg-nickname"
                  v-model="user.nickname"
                  required
                  class="form-input"
                  placeholder="请输入昵称"
              >
            </div>
          </div>
        </div>

        <div class="form-row">
          <!-- 邮箱 -->
          <div class="form-group">
            <label for="reg-email" class="form-label">邮箱</label>
            <div class="input-wrapper">
              <i class="icon-envelope"></i>
              <input
                  type="email"
                  id="reg-email"
                  v-model="user.email"
                  required
                  class="form-input"
                  placeholder="请输入邮箱"
              >
            </div>
          </div>

          <!-- 手机号 -->
          <div class="form-group">
            <label for="reg-phone" class="form-label">手机号</label>
            <div class="input-wrapper">
              <i class="icon-phone"></i>
              <input
                  type="tel"
                  id="reg-phone"
                  v-model="user.phone"
                  required
                  class="form-input"
                  placeholder="请输入手机号"
              >
            </div>
          </div>
        </div>

        <div class="form-row">
          <!-- 密码 -->
          <div class="form-group">
            <label for="reg-password" class="form-label">密码</label>
            <div class="input-wrapper">
              <i class="icon-lock"></i>
              <input
                  type="password"
                  id="reg-password"
                  v-model="user.password"
                  required
                  class="form-input"
                  placeholder="请输入密码"
              >
            </div>
          </div>

          <!-- 确认密码 -->
          <div class="form-group">
            <label for="reg-confirm" class="form-label">确认密码</label>
            <div class="input-wrapper">
              <i class="icon-lock"></i>
              <input
                  type="password"
                  id="reg-confirm"
                  v-model="confirmPassword"
                  required
                  class="form-input"
                  placeholder="请确认密码"
              >
            </div>
          </div>
        </div>

        <button type="submit" class="login-btn">注册</button>
        <div class="register-link">
          <span>已有账号？</span>
          <router-link to="/login" class="link">前往登录</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
// 保持原有注册逻辑不变
import { register } from '@/api/login'

export default {
  name: 'RegisterView',
  data() {
    return {
      user: {
        username: '',
        password: '',
        nickname: '',
        email: '',
        phone: ''
      },
      confirmPassword: ''
    }
  },
  methods: {
    register() {
      if (this.user.password !== this.confirmPassword) {
        alert('两次密码输入不一致')
        return
      }

      console.log('注册提交数据:', this.user)
      register(this.user).then(res => {
        if (res.data && res.data.code === 1) {
          alert('注册成功，请登录')
          this.$router.push('/login')
        } else {
          alert('注册失败: ' + (res.data?.msg || '未知错误'))
        }
      }).catch(err => {
        console.error('注册请求失败:', err)
        alert('注册失败，请检查网络或联系管理员')
      })
    }
  }
}
</script>

<style scoped>
/* 全局样式 */
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  box-sizing: border-box;
}

/* 登录盒子 (适应两列布局) */
.login-box {
  width: 100%;
  max-width: 700px; /* 适当加宽以容纳两列 */
  background-color: #ffffff;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  padding: 25px 30px;
  box-sizing: border-box;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.login-box:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
}

/* 登录头部 */
.login-header {
  text-align: center;
  margin-bottom: 25px;
}

.login-header h1 {
  font-size: 26px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.login-header p {
  font-size: 14px;
  color: #666;
  margin: 0;
}

/* 表单行 - 两列布局 */
.form-row {
  display: flex;
  gap: 15px; /* 两列之间的间距 */
  margin-bottom: 15px; /* 行与行之间的间距 */
}

/* 表单组 - 两列布局下各占50%宽度 */
.form-group {
  flex: 1; /* 平均分配宽度 */
  min-width: 0; /* 确保在小屏幕上能正确收缩 */
}

.form-label {
  display: block;
  margin-bottom: 5px;
  font-size: 14px;
  color: #555;
  font-weight: 500;
}

.input-wrapper {
  position: relative;
}

/* 图标样式 */
.icon-user::before { content: '👤'; }
.icon-lock::before { content: '🔒'; }
.icon-envelope::before { content: '✉️'; }
.icon-phone::before { content: '📱'; }

.icon-user, .icon-lock, .icon-envelope, .icon-phone {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: #888;
  font-size: 15px;
}

/* 输入框样式 */
.form-input {
  width: 100%;
  padding: 11px 12px 11px 40px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 14px;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
  box-sizing: border-box;
  height: 42px;
}

.form-input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
}

/* 注册按钮 */
.login-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.3s ease, transform 0.3s ease;
  width: 100%;
  margin-top: 10px;
}

.login-btn:hover {
  opacity: 0.9;
  transform: translateY(-2px);
}

.login-btn:active {
  transform: translateY(0);
}

/* 登录链接 */
.register-link {
  text-align: center;
  margin-top: 15px;
  font-size: 14px;
  color: #666;
}

.register-link .link {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s ease;
}

.register-link .link:hover {
  color: #556bda;
  text-decoration: underline;
}

/* 响应式调整 - 在小屏幕上自动变回单列 */
@media (max-width: 600px) {
  .login-box {
    max-width: 400px;
  }
  .form-row {
    flex-direction: column;
    gap: 0;
    margin-bottom: 0;
  }
  .form-group {
    margin-bottom: 15px;
  }
}
</style>